//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CompoundingRatePricer.h"
using namespace Cephei::QL::Experimental::Coupons;
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Experimental/Coupons/SubPeriodsPricer.h>
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (boost::shared_ptr<QuantLib::CompoundingRatePricer>& childNative, Object^ owner) : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
#ifdef HANDLE
	_phCompoundingRatePricer = NULL;
#endif
	_ppCompoundingRatePricer = &childNative;
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppCompoundingRatePricer));
}
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (QuantLib::CompoundingRatePricer& childNative, Object^ owner) : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
#ifdef HANDLE
	_phCompoundingRatePricer = NULL;
#endif
	_ppCompoundingRatePricer = new boost::shared_ptr<QuantLib::CompoundingRatePricer> (&childNative);
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppCompoundingRatePricer));
    _CompoundingRatePricerOwner = owner;
    _SubPeriodsPricerOwner = owner;
}

Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (CCompoundingRatePricer^ copy) : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
#ifdef HANDLE
	_phCompoundingRatePricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCompoundingRatePricer = new boost::shared_ptr<QuantLib::CompoundingRatePricer> (copy->GetShared());
        _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppCompoundingRatePricer));
    }
}
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (PLATFORM::Type^ t) : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
#ifdef HANDLE
	_phCompoundingRatePricer = NULL;
#endif
	if (!t->IsSubclassOf(CCompoundingRatePricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (QuantLib::Handle<QuantLib::CompoundingRatePricer>& childNative, Object^ owner)  : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
	_phCompoundingRatePricer = &childNative;
	_ppCompoundingRatePricer = &static_cast<boost::shared_ptr<QuantLib::CompoundingRatePricer>>(childNative.currentLink());
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppCompoundingRatePricer));
    _CompoundingRatePricerOwner = owner;
}
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (QuantLib::Handle<QuantLib::CompoundingRatePricer> childNative)  : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
	_phCompoundingRatePricer = &childNative;
	_ppCompoundingRatePricer = &static_cast<boost::shared_ptr<QuantLib::CompoundingRatePricer>>(childNative.currentLink());
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppCompoundingRatePricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::CCompoundingRatePricer (QuantLib::CompoundingRatePricer childNative)  : CSubPeriodsPricer(CCompoundingRatePricer::typeid)
{
#ifdef HANDLE
	_phCompoundingRatePricer = NULL;
#endif
	_ppCompoundingRatePricer = new boost::shared_ptr<QuantLib::CompoundingRatePricer> (new QuantLib::CompoundingRatePricer (childNative));
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppCompoundingRatePricer));
}
#endif

Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::~CCompoundingRatePricer ()
{
    if (_ppCompoundingRatePricer != NULL)
    {
	    delete _ppCompoundingRatePricer;
        _ppCompoundingRatePricer = NULL;
    }
}
Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::!CCompoundingRatePricer ()
{
    if (_ppCompoundingRatePricer != NULL)
    {
	    delete _ppCompoundingRatePricer;
    }
}
QuantLib::CompoundingRatePricer& Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::GetReference ()
{
    if (_ppCompoundingRatePricer == NULL) throw REFNEW NativeNullException ();
	return **_ppCompoundingRatePricer;
}
boost::shared_ptr<QuantLib::CompoundingRatePricer>& Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::GetShared ()
{
    if (_ppCompoundingRatePricer == NULL) throw REFNEW NativeNullException ();
	return *_ppCompoundingRatePricer;
}
QuantLib::CompoundingRatePricer* Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::GetPointer ()
{
    if (_ppCompoundingRatePricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppCompoundingRatePricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CompoundingRatePricer>& Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::GetHandle ()
{
	if (_phCompoundingRatePricer == NULL)
	{
		_phCompoundingRatePricer = new Handle<QuantLib::CompoundingRatePricer> (*_ppCompoundingRatePricer);
	}
	return *_phCompoundingRatePricer;
}
#endif
bool Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::HasNative () 
{
	return (_ppCompoundingRatePricer != NULL);
}

Double Cephei::QL::Experimental::Coupons::CCompoundingRatePricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCompoundingRatePricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

